package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.goods.TbGoodsDesc;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.vo.GoodsVO;
import com.dongdongshop.service.goods.GoodsService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/2/28 11:41
 */
@Service
@Component
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private TbGoodsMapper goodsMapper;

    @Resource
    private TbGoodsDescMapper goodsDescMapper;

    @Resource
    private TbItemMapper itemMapper;

    /**
     * 保存商品信息
     * @param goods
     * @param goodsDesc
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addGoods(TbGoods goods, TbGoodsDesc goodsDesc, List<TbItem> list) {
        //保存goods表并返回主键id
        goodsMapper.addGoods(goods);
        //保存goodsDes  goodsDes表的主键id不是自增的而是goods表的id值
        goodsDesc.setGoodsId(goods.getId());
        goodsDescMapper.insertSelective(goodsDesc);
        //保存item
        for (TbItem i : list) {
            i.setGoodsId(goods.getId());
            i.setUpdateTime(new Date());
        }
        itemMapper.addItemBatch(list);
    }

    /**
     * 查询未审核的商品信息
     * @return
     */
    @Override
    public List<GoodsVO> getZeroStatusGoodsVO() {
        return goodsMapper.getZeroStatusGoodsVO();
    }

    /**
     * 审核商品 status=1审核通过 status=2驳回
     */
    @Override
    public void updateBatchGoodsStatusById(Long[] ids, String status) {
        goodsMapper.updateBatchGoodsStatusById(ids,status);
    }

    /**
     * 根据商品id查询商品
     * @param id
     * @return
     */
    @Override
    public TbGoods getGoodsById(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }
}
